package com.ruoyi.aitafk.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.ServiceLog;
import com.ruoyi.aitcommon.api.DBOperation;
import com.ruoyi.aitcommon.service.IAitEntityInfoService;
import com.ruoyi.aitcommon.utils.EntityCache;
import com.ruoyi.aitcommon.utils.MapUtils;
import com.ruoyi.aitcommon.utils.SQLUtils;
import com.ruoyi.aitcommon.vo.DBConVO;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AFKSQLService {
    @Autowired
    DBOperation dboper;
    @Autowired
    IAitEntityInfoService infoService;
    @Autowired
    EntityCache entityCache;

    @ServiceLog("MAP更新服务")
    public int mapUpdate(List<Map<String, Object>> datas, JSONObject db, String tablename, String key1
            , String key2) throws Exception {
        if (db == null) throw new Exception("数据库配置为空");
        DBConVO db1 = JSONObject.toJavaObject(db, DBConVO.class);
        List<String> sqls = new ArrayList<>();
        String sqltemp = "UPDATE  PM_POLine set DescFlexSegments_PrivateDescSeg6=%s where id = (  select  line.ID from PM_PurchaseOrder  po\n" +
                "left JOIN PM_POLine line on line.PurchaseOrder=po.ID\n" +
                "where  po.DocNo ='%s' and line.DocLineNo=%s)";
        for (Map<String, Object> data : datas) {
            String docno = data.get("DocNo").toString();
            String lineno = data.get("DocLineNo").toString();
            String private6 = data.get("DescFlexField_PrivateDescSeg6").toString();

            String sql = String.format(sqltemp, private6, docno, lineno);
            sqls.add(sql);
        }
        return SQLUtils.execUpdate(sqls, db1);
    }

    @ServiceLog("MAP更新服务")
    public int mapUpdateLine(List<Map<String, Object>> datas, JSONObject db,  String key1
            , String key2) throws Exception {
        if (db == null) throw new Exception("数据库配置为空");
        DBConVO db1 = JSONObject.toJavaObject(db, DBConVO.class);
        List<String> sqls = new ArrayList<>();

        String sqltemp = "update MO_MOPickList set DescFlexField_PrivateDescSeg8= 'true' where id=(select  line.ID  from mo_mo doc  LEFT JOIN MO_MOPickList  line on line.MO=doc.ID" +
                " where doc.docno='%s' and line.doclineno= %s)";
        for (Map<String, Object> data : datas) {
            String docno = data.get("DocNo").toString();
            String lineno = data.get("DocLineNo").toString();

            String sql = String.format(sqltemp , docno, lineno);
            sqls.add(sql);
        }
        return SQLUtils.execUpdate(sqls, db1);
    }

    @ServiceLog("中间库更新服务")
    public int mapUpdateZJK(List<Map<String, Object>> datas, JSONObject db, String tablename, String key1, String key2, String key3, String key4) throws Exception {
        if (db == null) throw new Exception("数据库配置为空");
        DBConVO db1 = JSONObject.toJavaObject(db, DBConVO.class);
        List<String> sqls = new ArrayList<>();
        String sqltemp = "update " + tablename + " set coutno_id=" + key2 + ",status_flag=" + key3 + ",fail_reason=" + key4 + " where DocNo=" + key1 + "'";
        return SQLUtils.execUpdate(sqls, db1);
    }

}
